package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    //本地存储上传文件
//    @PostMapping("/upload")//上传文件，不是本地存储
//    public Result upload(String username,Integer age,@RequestParam("image") MultipartFile file) throws IOException {
//
//        log.info("username:{},age:{},file:{}",username,age,file.getOriginalFilename());
//        //构造一个唯一的文件名 uuid（通用的唯一标识符）
//
//        String originalFilename = file.getOriginalFilename();
//
//        int i = originalFilename.lastIndexOf(".");
//
//        String suffix = originalFilename.substring(i);
//
//
//        String newFilename = UUID.randomUUID().toString() + suffix;
//        log.info("newFilename:{}",newFilename);
//        //转存到磁盘文件里面去
//        file.transferTo(new java.io.File("D://"+newFilename));
//        return Result.success();

//
//    }
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")//上传文件，本地存储
    public Result upload(MultipartFile image) throws IOException {
        log.info("image:{}",image.getOriginalFilename());

        //调用阿里云工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("url:{}",url);
        return Result.success(url);

    }
}
